package com.hellogood.yaohuo.server.mgr;

import java.util.List;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.User;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class UserMgr {
	public User getUserByCode(String code) {
		return DAOFactory.getInstance().createUserDAO().getUserByCode(code);
	}

	public List<User> getAllUser() {
		return DAOFactory.getInstance().createUserDAO().getAllUser();
	}

	public String checkLogin(String user, char[] pwd) {

		String result = "";
		String staffCode = user.trim();
		String passwd = "";
		for (int i = 0; i < pwd.length; i++) {
			passwd = passwd + pwd[i];
		}
		passwd = Encryption.encryptStr(passwd.trim());

		User loginUser = DAOFactory.getInstance().createUserDAO().checkLogin(staffCode, passwd);

		if (loginUser == null) {
			return "登录错误，请检查用户和密码";
		} else {
			MainFrame.currentUser = loginUser;
			int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
			logID++;
			Log log = new Log();
			log.setLogId(logID);
			log.setLogDate(DateUtil.currentTime());
			log.setStaffCode(loginUser.getStaffCode());
			log.setStaffName(loginUser.getStaffName());
			log.setContent("登录");
			DAOFactory.getInstance().createLogDAO().addNewLog(log);
		}

		return result;
	}

	public User checkLogin(String username, String password) {
		User loginUser = null;
		if (username != null && password != null) {
			username = username.trim();
			password = password.trim();
			password = Encryption.encryptStr(password);
			loginUser = DAOFactory.getInstance().createUserDAO().checkLogin(username, password);
			if (loginUser == null) {
				
			} else {
				MainFrame.currentUser = loginUser;
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setLogId(logID);
				log.setLogDate(DateUtil.currentTime());
				log.setStaffCode(loginUser.getStaffCode());
				log.setStaffName(loginUser.getStaffName());
				log.setContent("登录");
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
		}
		return loginUser;
	}
}
